package telephone;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.zone.ZoneRules;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Time {
    private static final Logger log = LoggerFactory.getLogger(Time.class);
    private ZonedDateTime startingTime; //开始通话时间
    private ZonedDateTime endingTime;   //结束通话时间

    public Time(ZonedDateTime startingTime, ZonedDateTime endingTime) {
        this.startingTime = startingTime;
        this.endingTime = endingTime;
    }


    public long getTime() {
        long minute = 0;
        long between;

        ZoneId zoneId = startingTime.getZone();
        ZoneRules zoneRules = zoneId.getRules();

        Boolean isDstOfStart = zoneRules.isDaylightSavings( startingTime.toInstant() );
        Boolean isDstOfEnd = zoneRules.isDaylightSavings( endingTime.toInstant() );
        
        log.info("start:{},夏令时:{},end:{},夏令时:{}",
                startingTime.toLocalDateTime(),
                isDstOfStart,
                endingTime.toLocalDateTime(),
                isDstOfEnd
                );
        
        between = endingTime.toEpochSecond() - startingTime.toEpochSecond();

        if (between < 0)
            minute = between / 60;
        else
            minute = (between + 59) / 60;

        log.info("between:{},minutes:{}",between,minute);

        if (minute > 1800 || minute < 0) {
            if(endingTime.getHour() == 2 && startingTime.getHour() == 2 && startingTime.getMonthValue() == 11 && endingTime.getMonthValue() == 11 && !isDstOfStart && !isDstOfEnd)
                return minute + 60;
            throw new RuntimeException("通话时间不正确");
        } else {
            return minute;
        }
    }

    public double getprice(){
        double sumprice = 0;
        long sumtime = getTime();
        if(sumtime <= 20)
            sumprice = (double) sumtime * 0.05;
        else{
            sumprice += 1;
            sumprice += ((double) sumtime - 20.0) * 0.10;
        }
        log.info("sumprice:{}",sumprice);
        return sumprice;
    }
}